package com.wrupple.vegetate.server.process.impl;

import javax.servlet.http.HttpServletRequest;

import com.google.gwt.event.shared.EventBus;
import com.wrupple.muba.common.shared.Process;
import com.wrupple.muba.common.shared.SequentialProcess;
import com.wrupple.muba.common.shared.State;
import com.wrupple.muba.common.shared.StateTransition;
import com.wrupple.vegetate.server.integration.HttpServletResponseHandler;
import com.wrupple.vegetate.server.process.VegetateRequestHandlingProcess;
import com.wrupple.vegetate.server.state.RequestReader;
import com.wrupple.vegetate.server.state.ResponseWriter;

public class SimpleRequestHandlingProcess<T> extends SequentialProcess<HttpServletRequest, HttpServletResponseHandler> implements
		VegetateRequestHandlingProcess<T> {

	private RequestReader<T> interpret;
	private ResponseWriter<T> writer;
	private Process<T, T> process;

	public SimpleRequestHandlingProcess(RequestReader<T> reader, ResponseWriter<T> writer) {
		super();
		this.interpret= reader;
		this.writer = writer;
	}

	@Override
	public void start(HttpServletRequest parameter,
			StateTransition<HttpServletResponseHandler> onDone, EventBus bus) {
		add(interpret);
		if(process!=null){
			for(State<?, ?> state : process){
				add(state);
			}
		}
		add(writer);
		super.start(parameter, onDone, bus);
	}
	
	@Override
	public void setUnderlyingProcess(Process<T, T> process){
		this.process=process;
	}
	
	@Override
	public RequestReader<T> getInterpret() {
		return interpret;
	}

	@Override
	public ResponseWriter<T> getWriter() {
		return writer;
	}


}
